package sg.edu.nus.iss.jass.controller;

import java.util.List;

import sg.edu.nus.iss.jass.constant.ActionBtnContants;
import sg.edu.nus.iss.jass.constant.CommonConstants;
import sg.edu.nus.iss.jass.constant.ErrorConstants;
import sg.edu.nus.iss.jass.datastore.Transaction;
import sg.edu.nus.iss.jass.datastore.exception.DataStoreException;
import sg.edu.nus.iss.jass.dto.Action;
import sg.edu.nus.iss.jass.dto.Company;
import sg.edu.nus.iss.jass.dto.GeographicalLocation;
import sg.edu.nus.iss.jass.dto.JobAdvert;
import sg.edu.nus.iss.jass.dto.JobResponse;
import sg.edu.nus.iss.jass.dto.JobSeeker;
import sg.edu.nus.iss.jass.dto.Resume;
import sg.edu.nus.iss.jass.dto.Skill;
import sg.edu.nus.iss.jass.dto.SkillReference;
import sg.edu.nus.iss.jass.gui.ActionPanel;
import sg.edu.nus.iss.jass.gui.DefaultActionPanel;
import sg.edu.nus.iss.jass.gui.FormPanel;
import sg.edu.nus.iss.jass.gui.HeaderPanel;
import sg.edu.nus.iss.jass.gui.JobAdvertisementDetailFormPanel;
import sg.edu.nus.iss.jass.gui.JobAdvertisementFormPanel;
import sg.edu.nus.iss.jass.gui.JobAdvertisementModifyFormPanel;
import sg.edu.nus.iss.jass.gui.JobApplicationFormPanel;
import sg.edu.nus.iss.jass.gui.MainFrame;
import sg.edu.nus.iss.jass.persistence.ActionDAO;
import sg.edu.nus.iss.jass.persistence.CompanyDAO;
import sg.edu.nus.iss.jass.persistence.JobAdvertDAO;
import sg.edu.nus.iss.jass.persistence.JobResponseDAO;
import sg.edu.nus.iss.jass.persistence.JobSeekerDAO;
import sg.edu.nus.iss.jass.persistence.LocationDAO;
import sg.edu.nus.iss.jass.persistence.ResumeDAO;
import sg.edu.nus.iss.jass.persistence.SkillDAO;
import sg.edu.nus.iss.jass.persistence.UserDAO;
import sg.edu.nus.iss.jass.privilege.JobSeekerRole;
import sg.edu.nus.iss.jass.privilege.Role;
import sg.edu.nus.iss.jass.privilege.SessionContext;
import sg.edu.nus.iss.jass.privilege.User;


public class UserController {

	private UserDAO userDAO;
	
	public UserController(){
		userDAO = new UserDAO();
	}
	
	public void processAddJobSeekerUser(String username, String password) throws Exception {
		Transaction trans = null;
		try {
			trans = userDAO.getTransaction();
		} catch (DataStoreException e) {
			//TODO handle exception or throw exception
		}
		
		if(trans != null){
			try {
				trans.startTransaction();
				Role jobSeekerRole = new JobSeekerRole();
				jobSeekerRole.setId(CommonConstants.JOBSEEKER_ROLEID);
				User user = new User(username, password, jobSeekerRole);
				
				userDAO.addUser(user);
				trans.commit();
			} catch (DataStoreException e) {
				try {
					trans.rollback();
				} catch (DataStoreException e1) {
					e1.printStackTrace();
					throw e1;
				}
				throw e;
			}
		}
	}
	
	public User getUserById(String userID) {
		Transaction trans = null;
		User result = null;
		try {
			trans = userDAO.getTransaction();
		} catch (DataStoreException e) {
			//TODO handle exception or throw exception
		}
		
		if(trans != null){
			try {
				trans.startTransaction();
				result = userDAO.getUserByUserId(userID);
				trans.commit();
			} catch (DataStoreException e) {
				try {
					trans.rollback();
				} catch (DataStoreException e1) {
					e1.printStackTrace();
				}
			}
		}
		return result;
	}
	
}
